package org.vgou.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.vgou.dao.IBaseDao;

/**
* 
* Copyright (c) 2011, vgou<br>
* All rights reserved.<br>
* 
* @since 2011-11-27
* @author <a href="mailto:jason.hxx@gmail.com">jason</a>
* @version Id: BaseDaoImpl.java 
* @comment	数据库操作公共类
* 
*/
// 设置默认的事务管理策略，即没有标注
@Component("baseDao")
public class BaseDaoImpl extends AbstractManager implements IBaseDao {

	private static final Log log = LogFactory.getLog(BaseDaoImpl.class);

	/**
	 * 
	 * @param entity
	 * @return criteria
	 * @throws DataAccessException
	 */
	@Override
	public Criteria getCriteria(Class entity) throws DataAccessException {

		return super.getCriteria(entity);
	}

	/**
	 * 得到动态查询数据量
	 * 
	 * @param detachedCriteria
	 *            DetachedCriteria
	 * @return int
	 */
	@Override
	public int getCountByCriteria(DetachedCriteria detachedCriteria) {
		return super.getCountByCriteria(detachedCriteria);
	}

	/**
	 * 
	 * @param entity
	 * @param id
	 *            as Integer
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, Integer id) throws DataAccessException {
		Object obj = null;
		try {
			obj = super.getByPk(entity, id);
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);

		}
		return obj;
	}

	/**
	 * 
	 * @param entity
	 * @param id
	 *            as Long
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, Long id) throws DataAccessException {
		Object obj = null;
		try {
			obj = super.getByPk(entity, id);
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);
			throw exception;
		}
		return obj;
	}

	/**
	 * 
	 * @param entity
	 * @param id
	 *            as String
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class entity, String id) throws DataAccessException {

		Object obj = null;
		try {
			obj = super.getByPk(entity, id);
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);
			throw exception;
		}
		return obj;
	}

	/**
	 * 通过一个对象查找,该对象可以是一个复合键
	 * 
	 * @param entity
	 *            Class
	 * @param object
	 *            Serializable
	 * @return Object
	 * @throws DataAccessException
	 */
	@Override
	public Object getByPk(Class entity, Serializable object)
			throws DataAccessException {

		Object obj = null;
		try {
			obj = super.loadByPriKey(entity, object);
			return obj;
		} catch (DataAccessException exception) {
			log.error("查找 " + entity.getName() + " 实例到数据库失败", exception);
		}
		return obj;
	}

	/**
	 * create a date into DB
	 * 
	 * @param entity
	 * @throws DataAccessException
	 */
	public Object create(Object entity) throws DataAccessException {
		try {
			// getHibernateTemplate().save(entity);
			return super.save(entity);
		} catch (DataAccessException exception) {
			log.error("创建 " + entity.getClass().getName() + " 实例到数据库失败",
					exception);
			throw exception;
		}

	}

	/**
	 * 
	 */
	@Override
	public void saveOrUpdate(Object entity) throws DataAccessException {
		try {
			super.saveOrUpdate(entity);
		} catch (DataAccessException exception) {
			log.error("创建 " + entity.getClass().getName() + " 实例到数据库失败",
					exception);
			throw exception;
		}

	}

	/**
	 * create a date into DB
	 * 
	 * @param entity
	 * @throws DataAccessException
	 */
	@Override
	public void batchRemove(List list) throws DataAccessException {
		try {
			// getHibernateTemplate().save(entity);
			super.batchRemove(list);
		} catch (DataAccessException exception) {
			log.error("删除失败", exception);
			throw exception;
		}

	}

	/**
	 * update a date from DB
	 * 
	 * @param entity
	 * @throws DataAccessException
	 */
	@Override
	public void update(Object entity) throws DataAccessException {
		try {
			super.update(entity);
		} catch (DataAccessException exception) {
			log.error("更新 " + entity.getClass().getName() + " 实例到数据库失败",
					exception);
			throw exception;
		}
	}

	/**
	 * delete a date from DB
	 * 
	 * @param entity
	 * @throws DataAccessException
	 */
	@Override
	public void delete(Object entity) throws DataAccessException {

		try {
			super.delete(entity);
		} catch (DataAccessException exception) {
			log.error("从数据库删除 " + entity.getClass().getName() + " 实例失败",
					exception);
			throw exception;
		}

	}
	/**
	 * delete all dates from DB
	 * 
	 * @param entity
	 * @throws DataAccessException
	 */
	public void deleteAll(Class entity) throws DataAccessException {

		try {
			getHibernateTemplate().setCacheQueries(true);
			List result = getHibernateTemplate().loadAll(entity);
			getHibernateTemplate().deleteAll(result);

		} catch (DataAccessException exception) {
			log.error("从数据库删除 " + entity.getName() + " 的所有记录失败", exception);
			throw exception;
		}
	}

	/**
	 * delete all Collection from DB
	 * 
	 * @param Collection
	 *            entities
	 * @throws DataAccessException
	 */
	public void deleteAll(Collection entities) throws DataAccessException {

		try {
			getHibernateTemplate().setCacheQueries(true);
			getHibernateTemplate().deleteAll(entities);
		} catch (DataAccessException exception) {
			log.error("从数据库删除  的所有记录失败", exception);
			throw exception;
		}
	}

	/**
	 * 根据keyName，keyValue，查询得到list然后返回第一个值
	 * 
	 * @param Class
	 *            entity
	 * @param String
	 *            keyName
	 * @param Object
	 *            keyValue
	 * @return Object
	 * @throws DataAccessException
	 */
	@Override
	public Object loadByKey(Class entity, String keyName, Object keyValue)
			throws DataAccessException {

		try {
			getHibernateTemplate().setCacheQueries(true);
			List result = getHibernateTemplate().find(
					"from " + entity.getName() + " where " + keyName + " = ?",
					keyValue);
			if (result != null && result.size() > 0) {
				return result.get(0);
			} else {
				return null;
			}
		} catch (DataAccessException exception) {
			log.error("查询数据库 " + entity.getName(), exception);
			throw exception;
		}
	}

	/**
	 * 根据topicID，userPK，查询得到list然后返回第一个值 zyf
	 * 
	 * @param Class
	 *            entity
	 * @param long
	 *            userPK
	 * @param long
	 *            topicID
	 * @return Object
	 * @throws DataAccessException
	 */
	@Override
	public Object loadByIDAndPK(Class entity, long topicID, long userPK)
			throws DataAccessException {

		try {
			getHibernateTemplate().setCacheQueries(true);
			List result = getHibernateTemplate().find(
					"from " + entity.getName() + " where " + "topicID" + " ="+ topicID +" and " +"userPK"+"="+userPK
					);
			if (result != null && result.size() > 0) {
				return result.get(0);
			} else {
				return null;
			}
		} catch (DataAccessException exception) {
			log.error("查询数据库 " + entity.getName(), exception);
			throw exception;
		}
	}
	
	/**
	 * 
	 * @param entity
	 * @return List
	 * @throws DataAccessException
	 */
	public List loadAll(Class entity) throws DataAccessException {

		List list = null;
		try {
			getHibernateTemplate().setCacheQueries(true);
			list = getHibernateTemplate().loadAll(entity);
		} catch (DataAccessException exception) {
			log.error("从数据库查询" + entity.getName() + "失败");
			throw exception;
		}
		return list;
	}

	public Object loadByPK(Class clazz, String id) throws DataAccessException {

		Object object = null;
		try {
			getHibernateTemplate().setCacheQueries(true);
			object = super.loadByPriKey(clazz, id);
		} catch (DataAccessException exception) {
			log.error("从数据库查询" + clazz.getName() + "失败");
			throw exception;
		}
		return object;
	}

	@Override
	public List findByHql(String hql) throws DataAccessException {
		List list = new ArrayList();
		try {
			getHibernateTemplate().setCacheQueries(true);
			list = super.find(hql);
		} catch (DataAccessException exception) {
			log.error("从数据库查询失败");
			throw exception;
		}
		return list;
	}

	@Override
	public List getTotal(String hql, Date date) throws DataAccessException {
		List list = new ArrayList();
		try {
			getHibernateTemplate().setCacheQueries(true);
			list = super.getTotal(hql, date);
		} catch (DataAccessException exception) {
			log.error("从数据库查询失败");
			throw exception;
		}
		return list;
	}

	@Override
	public List findByNamedQuery(String namedQuery) throws DataAccessException {
		List list = null;
		try {
			getHibernateTemplate().setCacheQueries(true);
			list = super.findByNamedQuery(namedQuery);
		} catch (DataAccessException exception) {
			log.error("从数据库查询失败");
			throw exception;
		}
		return list;
	}
	
	@Resource(name = "hibernateTemplate")
	public void setBaseHibernateTemplate(HibernateTemplate hibernateTemplate) {
		super.setHibernateTemplate(hibernateTemplate);
	}
}
